package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityTemplateBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class TemplateActivity extends BaseAc<ActivityTemplateBinding> {

    public static int type;
    public static List<String> listPath = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_template;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTemplateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvTemplateSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvTemplateSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        if (type == 0) {
            Glide.with(mContext).load(listPath.get(0)).into(mDataBinding.ivTemplateImg11);
            Glide.with(mContext).load(listPath.get(1)).into(mDataBinding.ivTemplateImg12);
            Glide.with(mContext).load(listPath.get(2)).into(mDataBinding.ivTemplateImg13);
            Glide.with(mContext).load(listPath.get(3)).into(mDataBinding.ivTemplateImg14);
            mDataBinding.rlTemplate1.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            Glide.with(mContext).load(listPath.get(0)).into(mDataBinding.ivTemplateImg21);
            Glide.with(mContext).load(listPath.get(1)).into(mDataBinding.ivTemplateImg22);
            Glide.with(mContext).load(listPath.get(2)).into(mDataBinding.ivTemplateImg23);
            mDataBinding.rlTemplate2.setVisibility(View.VISIBLE);
        } else if (type == 2) {
            Glide.with(mContext).load(listPath.get(0)).into(mDataBinding.ivTemplateImg31);
            Glide.with(mContext).load(listPath.get(1)).into(mDataBinding.ivTemplateImg32);
            Glide.with(mContext).load(listPath.get(2)).into(mDataBinding.ivTemplateImg33);
            Glide.with(mContext).load(listPath.get(3)).into(mDataBinding.ivTemplateImg34);
            mDataBinding.rlTemplate3.setVisibility(View.VISIBLE);
        } else if (type == 3) {
            Glide.with(mContext).load(listPath.get(0)).into(mDataBinding.ivTemplateImg41);
            Glide.with(mContext).load(listPath.get(1)).into(mDataBinding.ivTemplateImg42);
            Glide.with(mContext).load(listPath.get(2)).into(mDataBinding.ivTemplateImg43);
            Glide.with(mContext).load(listPath.get(3)).into(mDataBinding.ivTemplateImg44);
            Glide.with(mContext).load(listPath.get(4)).into(mDataBinding.ivTemplateImg45);
            Glide.with(mContext).load(listPath.get(5)).into(mDataBinding.ivTemplateImg46);
            Glide.with(mContext).load(listPath.get(6)).into(mDataBinding.ivTemplateImg47);
            Glide.with(mContext).load(listPath.get(7)).into(mDataBinding.ivTemplateImg48);
            mDataBinding.rlTemplate4.setVisibility(View.VISIBLE);
        }
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlTemplateSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}